const domainName = 'https://www.yqwl.top/'
// const domainName = 'http://0.0.0.0:8000/'
// const domainName = 'http://121.40.144.248:8080'
// const domainName = 'https://121.40.144.248'
// const domainName = 'https://www.woxiangfei.top'





const oss_domain = 'https://www.yqwl.top/'
const header = {'content-type':'application/x-www-form-urlencoded'}
const parameter = {dataType: 'json', enableCache: true, enableHttp2: true, enableQuic: true, header : header, method: 'POST', responseType: 'text', timeout: 0}
const status_list = [{id:1,name:'申请进班'},{id:3,name:'在训'},{id:9,name:'初考'},{id:10,name:'补考'},{id:6,name:'已毕业'},{id:7,name:'暂停训练'},{id:8,name:'已退学'}]
const license_list = [{id:1, name:'小型无人机操控员执照'}, {id:2, name:'中型无人机操控员执照'}, {id:3, name:'大型无人机操控员执照'}]
const pilot_rank_list = [{id:1, name:'操控员等级'}, {id:2, name:'超视距等级'}, {id:3, name:'教员等级'}]
const uav_type_list = [{id:1, name:'飞机'}, {id:2, name:'垂直起降飞机'}, {id:3, name:'旋翼机'}, {id:4, name:'倾转旋翼机'}, {id:5, name:'飞艇'}, {id:6, name:'自由气球'}, {id:7, name:'滑翔机'}, {id:8, name:'特殊类'}]
const uav_level_list = [{id:1, name:'旋翼机-多旋翼'}, {id:2, name:'旋翼机-自转旋翼机'}, {id:3, name:'旋翼机-直升机'}, {id:4, name:'特殊类'}]

const pilot_shortNames = [{id:1, name:'操'}, {id:2, name:'超'}, {id:3, name:'教'}]

const status_bgColors = [{id:1,name:'#ff0000'},{id:3,name:'#00b050'},{id:6,name:'goldenrod'},{id:7,name:'#999999'},{id:8,name:'#999999'},{id:9,name:'blue'},{id:10,name:'purple'}]

const pilot_bgColors = [{id:1,name:'purple'},{id:2,name:'goldenrod'},{id:3,name:'blue'}]

const nation_list = [{'id': 1, 'name': '汉族'}, {'id': 2, 'name': '蒙古族'}, {'id': 3, 'name': '回族'}, {'id': 4, 'name': '藏族'}, {'id': 5, 'name': '维吾尔族'}, {'id': 6, 'name': '苗族'}, {'id': 7, 'name': '彝族'}, {'id': 8, 'name': '壮族'}, {'id': 9, 'name': '布依族'}, {'id': 10, 'name': '朝鲜族'}, {'id': 11, 'name': '满族'}, {'id': 12, 'name': '侗族'}, {'id': 13, 'name': '瑶族'}, {'id': 14, 'name': '白族'}, {'id': 15, 'name': '土家族'}, {'id': 16, 'name': '哈尼族'}, {'id': 17, 'name': '哈萨克族'}, {'id': 18, 'name': '傣族'}, {'id': 19, 'name': '黎族'}, {'id': 20, 'name': '僳僳族'}, {'id': 21, 'name': '佤族'}, {'id': 22, 'name': '畲族'}, {'id': 23, 'name': '高山族'}, {'id': 24, 'name': '拉祜族'}, {'id': 25, 'name': '水族'}, {'id': 26, 'name': ' 东乡族'}, {'id': 27, 'name': '纳西族'}, {'id': 28, 'name': '景颇族'}, {'id': 29, 'name': '柯尔克孜族'}, {'id': 30, 'name': '土族'}, {'id': 31, 'name': '达斡尔族'}, {'id': 32, 'name': '仫佬族'}, {'id': 33, 'name': '羌族'}, {'id': 34, 'name': '布朗族'}, {'id': 35, 'name': '撒拉族'}, {'id': 36, 'name': '毛南族'}, {'id': 37, 'name': '仡佬族'}, {'id': 38, 'name': '锡伯族'}, {'id': 39, 'name': '阿昌族'}, {'id': 40, 'name': '普米族'}, {'id': 41, 'name': '塔吉克族'}, {'id': 42, 'name': '怒族'}, {'id': 43, 'name': '乌孜别克族'}, {'id': 44, 'name': '俄罗斯族'}, {'id': 45, 'name': '鄂温克族'}, {'id': 46, 'name': '德昂族'}, {'id': 47, 'name': '保安族'}, {'id': 48, 'name': '裕固族'}, {'id': 49, 'name': '京族'}, {'id': 50, 'name': '塔塔尔族'}, {'id': 51, 'name': '独龙族'}, {'id': 52, 'name': '鄂伦春族'}, {'id': 53, 'name': '赫哲族'}, {'id': 54, 'name': '门巴族'}, {'id': 55, 'name': '珞巴族'}, {'id': 56, 'name': '基诺族'}]

// app.js
App({
  globalData: {
    domainName,
    parameter,
    oss_domain,
    header,
    status_list,
    status_bgColors,
    license_list,
    uav_type_list,
    uav_level_list,
    pilot_rank_list,
    pilot_bgColors,
    pilot_shortNames,
    nation_list,
  },
  // 震动
  get_vibration(){
    if(!wx.getStorageSync('off_vibration')){
      wx.vibrateShort({
        type: 'light',
      })
    }
  },

  // 获取用户手机号
  getphonenumber(user_id,phone_code, navigation_action){
    wx.showLoading({
      title: '正在授权...',
    })
    // console.log('获取用户手机号',e)
    // 前往服务器，获取APP_SECRET
    wx.request({
      url: domainName + '/get_phoneNumber',
      data: {
        user_id:user_id,
        phone_code:phone_code,
      },...parameter,
      success: res => {
        wx.hideLoading()
        var update_res = res.data.update_res
        // console.log('acc_phone',res.data.phone)
        if(update_res){
          // getApp().globalData.has_phone = true
          // wx.setStorageSync('has_phone', true)
          wx.showToast({
            title: '授权成功',
            duration: 1500,
            icon: 'success',
            mask: false,
          })
          setTimeout(() => {
            if(navigation_action=='navigateBack'){
              wx.navigateBack()
            }
          }, 1500);
        }else{
          wx.showModal({
            content: '手机验证失败，稍后重试',
            showCancel: false,
            title: '授权失败',
          })
        }
      },
      fail:err=>{
        console.log('获取失败', err)
      }
    })
  },

  // 联系教务开通权限
  apply_get_right(){
    if(this.globalData.show_mall){
      wx.showModal({
        cancelColor:'#999',
        cancelText:'返回首页',
        confirmColor:'#00b050',
        confirmText:'前往开通',
        content: '暂无此权限',
        showCancel: true,
        title: '提示',
        success: res => {
          if(res.confirm){
            wx.redirectTo({
              url: '/pages/pay_opt/pay_opt',
            })
          }else{
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        },
      })
    }else{
      wx.showModal({
        cancelColor:'#999',
        cancelText:'返回',
        confirmColor:'#00b050',
        confirmText:'联系',
        content: '请联系咨询师，咨询开通权限',
        showCancel: true,
        title: '提示',
        success: res => {
          if(res.confirm){
            wx.redirectTo({
              url: '/pages/contact_admin/contact_admin',
            })
          }else{
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        },
      })
    }
  },

  // 声音
  get_sound(){
    myaudio.src="/imgs/sou.mp3"
    myaudio.play()
  },

  // 超时连接
  timeout_reconnect(errMsg,that){
    // console.log('errMsg',errMsg)
    wx.hideLoading()
    if('timeout'.indexOf(errMsg)){
      wx.showModal({
        confirmText: '重新连接',
        showCancel: true,
        title: '网络连接超时',
        success: res => {
          if(res.confirm){
            that.onLoad(that.options)
          }
        },
      })
    }
  },

  // 数组随机
  randomSort(arr) {
    var result = [];
    while (arr.length > 0) {
      var randomIndex = Math.floor(Math.random() * arr.length);
      result.push(arr[randomIndex]);
      arr.splice(randomIndex, 1);
    }
    return result;
  },

  // 将题库中的选项随机乱序
  random_optList_of_tiku(tiku){
    if(tiku.length>0){
      for(var i=0; i<tiku.length; i++){
        let arr = [
          {no:'A', opt:tiku[i].a},
          {no:'B', opt:tiku[i].b},
          {no:'C', opt:tiku[i].c},
        ]
        tiku[i].opt_list = this.randomSort(arr)
      }
    }
    return tiku
  },


  // 自动更新
  autoUpdate(){
    const updateManager = wx.getUpdateManager()

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      console.log('请求完新版本信息的回调',res.hasUpdate)
    })
    
    updateManager.onUpdateReady(function () {
      wx.showModal({
        title: '更新提示',
        content: '新版本已经准备好，是否重启应用？',
        success: function (res) {
          if (res.confirm) {
            // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
            updateManager.applyUpdate()
          }
        }
      })
    })
    
    updateManager.onUpdateFailed(function () {
      // 新版本下载失败
    })
  },

  onLaunch() {
    this.autoUpdate()
  },
})
